/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.zx.certificate.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.zx.certificate.entity.Tcertificate;
import com.jeesite.modules.zx.certificate.service.TcertificateService;
import com.jeesite.modules.zx.user.entity.ZxUser;
import com.jeesite.modules.zx.user.service.ZxUserService;

/**
 * 证书管理Controller
 * @author zpp
 * @version 2020-02-20
 */
@Controller
@RequestMapping(value = "${adminPath}/zx/certificate/tcertificate")
public class TcertificateController extends BaseController {

	@Autowired
	private TcertificateService tcertificateService;
	@Autowired
	private ZxUserService zxUserService;
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public Tcertificate get(String id, boolean isNewRecord) {
		return tcertificateService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("zx:certificate:tcertificate:view")
	@RequestMapping(value = {"list", ""})
	public String list(Tcertificate tcertificate, Model model) {
		model.addAttribute("tcertificate", tcertificate);
		return "modules/zx/certificate/tcertificateList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("zx:certificate:tcertificate:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<Tcertificate> listData(Tcertificate tcertificate, HttpServletRequest request, HttpServletResponse response) {
		tcertificate.setPage(new Page<>(request, response));
		Page<Tcertificate> page = tcertificateService.findPage(tcertificate);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("zx:certificate:tcertificate:view")
	@RequestMapping(value = "form")
	public String form(Tcertificate tcertificate, Model model) {
		
		ZxUser entity=new ZxUser();
		entity.setIdentity("student");
		List<ZxUser> findList = zxUserService.findList(entity);
		model.addAttribute("userList", findList);
		model.addAttribute("tcertificate", tcertificate);
		return "modules/zx/certificate/tcertificateForm";
	}

	/**
	 * 保存证书管理
	 */
	@RequiresPermissions("zx:certificate:tcertificate:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated Tcertificate tcertificate) {
		tcertificateService.save(tcertificate);
		return renderResult(Global.TRUE, text("保存证书管理成功！"));
	}
	
	/**
	 * 停用证书管理
	 */
	@RequiresPermissions("zx:certificate:tcertificate:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(Tcertificate tcertificate) {
		tcertificate.setStatus(Tcertificate.STATUS_DISABLE);
		tcertificateService.updateStatus(tcertificate);
		return renderResult(Global.TRUE, text("停用证书管理成功"));
	}
	
	/**
	 * 启用证书管理
	 */
	@RequiresPermissions("zx:certificate:tcertificate:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(Tcertificate tcertificate) {
		tcertificate.setStatus(Tcertificate.STATUS_NORMAL);
		tcertificateService.updateStatus(tcertificate);
		return renderResult(Global.TRUE, text("启用证书管理成功"));
	}
	
	/**
	 * 删除证书管理
	 */
	@RequiresPermissions("zx:certificate:tcertificate:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(Tcertificate tcertificate) {
		tcertificateService.delete(tcertificate);
		return renderResult(Global.TRUE, text("删除证书管理成功！"));
	}
	
}